<script setup>
import {reactive, ref} from "vue";
import { useAclStore } from "@/stores/aclStore.js";
import {ElMessage} from "element-plus";

const aclStore = useAclStore();
const dialogVisible = ref(false)
const createAcl = reactive({
  name:'',
  code:'',
  type: '',
  url: '',
})
const rules = ref({
  name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
  code: [{ required: true, message: '请输入权限码', trigger: 'blur' }],
  type: [{ required: true, message: '请选择权限类型', trigger: 'blur' }],
  url: [{ required: true, message: '请输入资源路径', trigger: 'blur' }],
})
const createAclFormRef = ref(null)

const submitForm = () => {
  createAclFormRef.value?.validate().then(() => {
    console.log('校验通过2222')
    aclStore.createAcl({
      ...createAcl
    })
  }).catch(() => {
    ElMessage('请填写完整信息')
  })
}
const show = () => {
  dialogVisible.value = true
}
//对外暴露show
defineExpose({
  show
})
</script>

<template>
  <el-dialog
      title="创建权限"
      v-model="dialogVisible"
      width="50%"
  >
    <el-form :model="createAcl" :rules="rules" ref="createAclFormRef" label-width="100px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="createAcl.name" placeholder="请输入权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限码" prop="code">
        <el-input v-model="createAcl.code" placeholder="请输入权限码"></el-input>
      </el-form-item>
      <el-form-item label="权限类型" prop="type">
        <el-select v-model="createAcl.type" placeholder="请选择权限类型">
          <el-option label="API" value="1"></el-option>
          <el-option label="菜单" value="2"></el-option>
          <el-option label="按钮" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="资源路径" prop="url">
        <el-input v-model="createAcl.url" placeholder="请输入资源路径"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="dialogVisible = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<style scoped>

</style>